<?php

namespace App\Repositories;

use App\Exceptions\BasicException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use function Matrix\add;    //引入hash

class VirusWinPrizeDetailRepository {

    /*
     *获取礼物的数量总和
     * @param $type
     * @param $gift_id
     * @return bool
     * @throws BasicException
     * */
    public  function  totalNumber($type,$gift_id){
        $totalNumber = app('model_virus_win_prize_detail')->where(['pool_type'=>$type,'gift_id'=>$gift_id,'type'=>2])->sum('number');
        return $totalNumber ?? 0;
    }

        /*
     *获取该分类奖池的所有中奖个数
     * @param $type
     * @return bool
     * @throws BasicException
     * */
    public  function  totalTypeNumber($type){
        $totalTypeNumber = app('model_virus_win_prize_detail')->where(['pool_type'=>$type,'type'=>2])->sum('number');
        return $totalTypeNumber ?? 0;
    }

      /*
      *获取礼物价值金币总和
      * @param $type
      * @param $gift_id
      * @return bool
      * @throws BasicException
      * */
    public  function  totalGold($type,$gift_id){
        $totalGold = app('model_virus_win_prize_detail')->where(['pool_type'=>$type,'gift_id'=>$gift_id,'type'=>2])->sum('gold_coin');
        return $totalGold ?? 0;
    }

     /*
    *获取中奖列表
    * @param $where
    * @param $keyword
    * @param $page
    * @param $perPage
    * @return bool
    * @throws BasicException
    * */
    public  function  winGiftList($where,$keyword,$page=1,$perPage=20){
        $winGiftList = app('model_virus_win_prize_detail')
            ->with(['gift:id,name,price,picture', 'user_data:id,user_id,user_code,nickname'])
            ->orderBy('id', 'desc')
            ->select('id','user_id','number','gift_id','add_time')
            ->where($where)
            ->where(function ($query) use ($keyword) {
                $query->whereHas('user_data', function ($query) use ($keyword) {
                    $query->where('user_code', 'like', "%{$keyword}%")->orWhere('nickname', 'like', "%{$keyword}%");
                });
            })
            ->paginate($perPage, ['*'], '', $page);
        return $winGiftList;
    }


}

?>